<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2020-08-01
 * Time: 下午 08:38
 */
namespace Action\Page;

use Action\Common\BaseLayout;

/**
 * Class Edit
 * @property-read \Model\Page $pageModel
 * @package Action\Page
 */
class Edit extends BaseLayout {
    public $templates;
    protected $_isEdit;
    public $pageData;
    public $pageParams;
    public $templateSelect;
    public $error;
    
    public function main() {
        $this->addBreadcrumb('页面', '/');
        
        $template = $_REQUEST['template'];
        $id = $_REQUEST['id'];
        $info = [];
        
        if($id) {
            if(!$this->pageModel->exists($id)) {
                $this->setMessage("页面不存在");
                $this->view();
            }
            
            $info = $this->pageModel->getInfo($id);
            $template = $info['template'];
            $this->_isEdit = true;
            $this->templateData['header']['title'] = '编辑页面 "'. $info['name'] .'"';
            $this->addBreadcrumb('编辑页面');
        } else {
            $this->templateData['header']['title'] = '新增页面';
            $this->addBreadcrumb('选择模板', $this->fns->newUrlCreator()->removeQueryParam('template'));
                
            if(!$this->hasTemplate()) {
                $this->templateSelect = true;
                $this->templateData['header']['title'] = '新增页面 - 选择模板';
            }
            
            if($this->hasTemplate()) {
                $this->addBreadcrumb('新增页面');
            }
        }
        
        $this->pageParams = $this->pageModel->getTemplateInfo($template);
        $this->pageData = $info;
        $this->pageData['template'] = $template;
        
        $this->view();
    }
    
    public function hasTemplate() {
        return !empty($_REQUEST['template']);
    }

    /**
     * 获取页面模板设置
     * @return array|null
     */
    public function getPageTemplates() {
        if(!$this->templates) {
            $this->templates = $this->pageModel->getTemplates();
        }

        return $this->templates;
    }
    
    public function isEdit() {
        return $this->_isEdit;
    }
} 